package com.hsyt.pms.controller.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hsyt.pfc.support.rpc.PageResult;
import com.hsyt.pfc.support.util.LoggerUtil;
import com.hsyt.pms.api.common.PltAuth;
import com.hsyt.pms.api.common.PltLog;
import com.hsyt.pms.api.common.ResultMessage;
import com.hsyt.pms.api.dto.system.SysPersonDto;
import com.hsyt.pms.api.enums.AbleStatus;
import com.hsyt.pms.api.vo.system.SysPersonVo;
import com.hsyt.pms.controller.base.BaseController;
import com.hsyt.pms.sys.dubbo.SysPersonService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.MessageFormat;
import java.util.Iterator;
import java.util.List;

/**
 * @ClassName: SysPersonController
 * @Description: 人员控制层
 * @author: wangze@hsyuntai.com
 * @date: 2019/1/29 18:19
 */
@RestController
@RequestMapping("/pms/system/person")
public class SysPersonController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysPersonController.class);

    @Reference(group = "pmsSystem", registry = "pms", version = "0.0.1")
    private SysPersonService sysPersonService;

    /**
     * 分页列表页面跳转
     *
     * @author wangze@hsyuntai.com
     * @date 2019年2月12日 下午2:45:51
     * @return
     */
    @RequestMapping("/paginationList")
    public String paginationList() {
        return "/person/paginationList";
    }

    /**
     * form表单跳转，新增页面或编辑页面
     *
     * @author wangze@hsyuntai.com
     * @date 2019年2月12日 下午2:45:51
     * @return
     */
    @RequestMapping("/form")
    @PltAuth(auth = "ROLE_USER_MG_MODIFY,ROLE_USER_MG_MODIFY,ROLE_USER_MG_FROZEN,ROLE_USER_MG_STOP")
    public String form(String formType, Model model) {
        model.addAttribute("formType", formType);
        return "/person/form";
    }

    /**
     * 分页列表条件搜索
     *
     * @author wangze@hsyuntai.com
     * @date 2019年2月12日 下午2:45:51
     * @param dto
     * @return
     */
    @RequestMapping("/searchPageData")
    @PltAuth(auth = "ROLE_PERSON_MG")
    @ResponseBody
    public PageResult<SysPersonVo> searchPageData(@RequestBody SysPersonDto dto) {
        PageResult<SysPersonVo> pageResult = new PageResult<SysPersonVo>();
        try {
            if (dto != null && StringUtils.isNotBlank(dto.getName())) {
                dto.setName(dto.getName().replaceAll("%", "\\\\%"));
            }
            if (dto != null && StringUtils.isNotBlank(dto.getIdCardNo())) {
                dto.setIdCardNo(dto.getIdCardNo().replaceAll("%", "\\\\%"));
            }
            if (dto != null && StringUtils.isNotBlank(dto.getDepartment())) {
                dto.setDepartment(dto.getDepartment().replaceAll("%", "\\\\%"));
            }
            pageResult = sysPersonService.searchPageDataByDto(dto);
        } catch (Exception e) {
            pageResult.setResult(false);
            String message = MessageFormat.format("SysPersonController.searchPageData error: {0}", e.getMessage());
            pageResult.setMessage(message);
            LoggerUtil.error(e, LOGGER, message);
        }
        return pageResult;
    }

    @PltLog(log = "根据ds集合删除数据项")
    @PltAuth(auth = "ROLE_USER_MG_FROZEN,ROLE_USER_MG_STOP")
    @RequestMapping(value = "/deleteByIds")
    public @ResponseBody
    ResultMessage deleteByIds(@RequestBody List<Long> ids) {
        ResultMessage result = new ResultMessage(true, "成功");
        if (ids == null || ids.size() <= 0) {
            result.change(false, "入参有误" + ids);
            return result;
        }
        try {
            for (Iterator<Long> iterator = ids.iterator(); iterator.hasNext();) {
                Long id = iterator.next();
                SysPersonDto dto = new SysPersonDto();
                dto.setId(id);
                dto.setStatus(AbleStatus.DISABLED.getCode());
                sysPersonService.updatePerson(dto);
            }
        } catch (Exception e) {
            result.change(false, "操作失败，错误信息:" + e.getMessage());
            LoggerUtil.error(e, LOGGER, "SysPersonController.delete error: {0}", e.getMessage());
        }
        return result;
    }

    @PltLog(log = "新增人员")
    @PltAuth(auth = "ROLE_USER_MG_ADD")
    @RequestMapping(value = "/add")
    public @ResponseBody
    ResultMessage add(@RequestBody SysPersonDto dto) {
        ResultMessage result = new ResultMessage(true, "新增成功");
        try {
            if (StringUtils.isBlank(dto.getStatus())) {
                dto.setStatus(AbleStatus.ENABLED.getCode());
            }
            sysPersonService.insertPerson(dto);
        } catch (Exception e) {
            result.change(false, "保存失败 , 错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysPersonController.add error: {0}", e.getMessage());
        }
        return result;
    }

    @PltLog(log = "编辑人员")
    @PltAuth(auth = "ROLE_USER_MG_MODIFY")
    @RequestMapping(value = "/update")
    public @ResponseBody
    ResultMessage update(@RequestBody SysPersonDto dto) {
        ResultMessage result = new ResultMessage(true, "编辑成功");
        try {
            sysPersonService.updatePerson(dto);
        } catch (Exception e) {
            result.change(false, "保存失败 , 错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysPersonController.update error: {0}", e.getMessage());
        }
        return result;
    }

}
